package com.easyaop.api.join;

import com.easyaop.api.event.BeforeEvent;
import com.easyaop.api.event.ReturnEvent;
import com.easyaop.api.event.ThrowableEvent;
import com.easyaop.api.event.listener.MethodListener;

public class JoinImpl extends Join implements BeforeEvent, ReturnEvent, ThrowableEvent {
    private MethodListener[] listeners;

    private Object[] listenerData;

    public int listenerIndex = 0;

    boolean isChangReturn = false;

    boolean isSkipOriginMethod = false;

    public JoinImpl(String method, Object target, Object[] args) {
        this.arguments = args;
        this.signature = method;
        this.This = target;
    }

    public void setListeners(MethodListener[] listeners) {
        this.listeners = listeners;
        listenerData = new Object[listeners.length];
    }

    public MethodListener[] getListeners() {
        return listeners;
    }

    public boolean isChangReturn() {
        return isChangReturn;
    }

    @Override
    public void $return(Object ret) {
        isChangReturn = true;
        returnValue = ret;
    }

    public boolean isSkipOriginMethod() {
        return isSkipOriginMethod;
    }

    @Override
    public void skipOriginMethod(Object ret) {
        isSkipOriginMethod = true;
        isChangReturn = true;
        returnValue = ret;
    }

    @Override
    public void $catch(Object ret) {

    }

    public void setData(Object listenerData) {
        this.listenerData[listenerIndex] = listenerData;
    }

    public Object getData() {
        return this.listenerData[listenerIndex];
    }

    public static JoinImpl of(Class clz, String method, Object target, Object[] args) {
        JoinImpl join = new JoinImpl(method, target, args);
        join.clz = clz;
        return join;
    }
}
